package DigiJoe;

import java.util.ArrayList;
import java.util.HashMap;


public abstract class DigitalGate extends DigitalElement {

    //bemenetek listaja
    protected HashMap<Integer, ArrayList<Wire>> inputList = new HashMap<Integer, ArrayList<Wire>>();
    //max bemenetek szama
    protected int maxInputPort;

    public DigitalGate (int MaxInputPort, int MaxOutputPort) {
        super(MaxOutputPort);
        //bemenet beallitasa
        maxInputPort = MaxInputPort;
    }
    
    //minden bekotott vezeteket ad vissza
    public ArrayList<Wire> GetAllInput(){
        ArrayList<Wire> temp = new ArrayList<Wire>();
        for(int i = 0; i < inputList.size(); i++){
            temp.addAll(inputList.get(i));
        }
        return temp;
    }

    public int getMaxInputPort() { return maxInputPort; }

    //abstract gatelogika
    public abstract void gateLogic () throws Exception;

    //input hozzaadasa
    public void addInput (int port, Wire input) throws Exception {
        //ellenorzes
        if(port >= maxInputPort)
            throw new Exception("The port given is too high!");
            //ErrorController.signalError("The port given is too high!");
        if (port < 0)
            throw new Exception("The port given is too high!");
            //ErrorController.signalError("The port given is too low!");

        //ha nem letezik a port
        if(!inputList.containsKey(port)){
            inputList.put(port, new ArrayList<Wire>());
            //1 portra egy bemenet johet csak
            inputList.get(port).add(input);
        }
    }
}
